/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;


import entities.Supplier;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Andrés Felipe
 */
@Stateless
@Remote(SupplierFacadeRemote.class)
public class SupplierFacade extends AbstractFacade<Supplier> implements SupplierFacadeRemote {
    @PersistenceContext(unitName = "Javeriana.EJB.EstimatorPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public SupplierFacade() {
        super(Supplier.class);
    }
    
    @Override
    public List<String> getSupplierInformation(int supplierIdentification) {
         List<String> row = new ArrayList<>();    
         Supplier supplier = (Supplier) this.find(supplierIdentification);
         if (supplier == null) {
             //System.out.println("No lo encontré!!!");
             return new ArrayList<>();
         }
         row.add(supplier.getName());
         row.add(supplier.getEmail());
         row.add(supplier.getPhone());
         row.add(supplier.getAddress());
         row.add(supplier.getIdentification().toString());
         //System.out.println("Lo encontré!!!");
         return row; 
     }

    @Override
    public List<String> findSuppliersByMaterial(String material, int pymeIdentification) {
        Query query = em.createNamedQuery("SupplierXpyme.findByPyme");
        query.setParameter("identification", pymeIdentification);
        List<Supplier> suppliers = (List<Supplier>)query.getResultList();
        List<String> suppliersNames = new ArrayList<>();
        for (Supplier supplier : suppliers) {
            if(supplier.getMaterial().getTypematerial().equals(material)) {
                suppliersNames.add(supplier.getIdentification() + "-" + supplier.getName());
            }
        }
        
        return suppliersNames;
    }
    
    @Override
    public int findIdByEmail(String email) {
        Query query = em.createNamedQuery("Supplier.findByEmail");
        query.setParameter("email", email);
        Supplier supplier = (Supplier)query.getSingleResult();
        return supplier.getIdentification();
    }

    @Override
    public List<String> findAllSuppliers() {
        List<String> result = new ArrayList<>();
        List<Supplier> supliers = this.findAll();
        for (Supplier supplier : supliers) {
            String temp = supplier.getIdentification() + "-" + supplier.getName();;
            result.add(temp);
        }
        return result;
    }
    
}
